//
//  MailboxBindingVC.m
//  MRBang
//
//  Created by 铅笔 on 15/12/16.
//  Copyright © 2015年 杭州易网新科技. All rights reserved.
//

#import "MailboxBindingVC.h"
#import "MailboxBindingSecondVC.h"

@interface MailboxBindingVC ()

@property (nonatomic,strong) UITextField *textField_email;

@property (nonatomic,strong) UIButton *button_getCheckCode;

@end

@implementation MailboxBindingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建子视图
    [self initWithAllSubviews];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    self.title = self.str_title;
}

#pragma mark ---- 创建子视图
- (void) initWithAllSubviews
{
    //label 提示字样
    UILabel *label_text = [[UILabel alloc] initWithFrame:CGRectMake(46*PROPORTION_WIDTH,64+40*PROPORTION_WIDTH, kScreenWidth-40, 30)];
    [self.view addSubview:label_text];
    label_text.text = @"请使用常用邮箱,方便接收信息及密码找回";
    label_text.font = [UIFont systemFontOfSize:Font_SIZE_22];
    label_text.textColor = COLOT_TEXTGRAY;
    
    self.textField_email = [[UITextField alloc] initWithFrame:CGRectMake(20,label_text.bottom+22*PROPORTION_WIDTH,kScreenWidth - 40, 68*PROPORTION_WIDTH)];
    [self.view addSubview:self.textField_email];
    self.textField_email.placeholder = @"  请输入邮箱";
    self.textField_email.font = [UIFont systemFontOfSize:Font_SIZE_24];
    
    self.textField_email.layer.borderColor = RXDColor(228, 228, 228).CGColor;
    self.textField_email.layer.borderWidth = 1.0;
    self.textField_email.layer.cornerRadius = 5.0;
    self.textField_email.clipsToBounds = YES;
    
    //设置修改button
    self.button_getCheckCode = [[UIButton alloc] initWithFrame:CGRectMake(20, self.textField_email.bottom+56*PROPORTION_WIDTH,kScreenWidth - 40, 93*PROPORTION_WIDTH)];
    [self.view addSubview:self.button_getCheckCode];
    self.button_getCheckCode.backgroundColor = RXDColor(255, 118, 112);
    
    if ([self.str_fromeWhere isEqualToString:@"tradePwd"]) {
        [self.button_getCheckCode setTitle:@"找回" forState:UIControlStateNormal];
    }
    else
    {
        [self.button_getCheckCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        //下一步
//      UIButton * button_next = [[UIButton alloc] initWithFrame:CGRectMake(20, self.button_getCheckCode.bottom+56*PROPORTION_WIDTH,kScreenWidth - 40, 93*PROPORTION_WIDTH)];
//        [self.view addSubview:button_next];
//        button_next.backgroundColor = RXDColor(255, 118, 112);
//         [button_next setTitle:@"下一步" forState:UIControlStateNormal];
//        
//        [button_next.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
//        [button_next addTarget:self action:@selector(nextButtonAction) forControlEvents:UIControlEventTouchUpInside];
//        
//        button_next.layer.cornerRadius = 3.0;
//        button_next.clipsToBounds = YES;

    }
    
    [self.button_getCheckCode.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    [self.button_getCheckCode addTarget:self action:@selector(getCheckCodeButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.button_getCheckCode.layer.cornerRadius = 3.0;
    self.button_getCheckCode.clipsToBounds = YES;
    
}

#pragma mark ---- 获取验证码
- (void) getCheckCodeButton
{
    if (self.textField_email.text.length==0) {
        [UIView showAlertMsg:@"请填写完整信息"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary *dic_email = @{@"email":self.textField_email.text};
    
    NSString *strUrl;
    if ([self.str_title isEqualToString:@"邮箱找回密码"]) {
        strUrl = [NSString stringWithFormat:@"%@member/password/findbyemail",MRAPI];
    }
    if ([self.str_title isEqualToString:@"邮箱绑定"]) {
        strUrl = [NSString stringWithFormat:@"%@captcha/email",MRAPI];
    }
    if ([self.str_fromeWhere isEqualToString:@"tradePwd"]) {
        strUrl = [NSString stringWithFormat:@"%@member/paypassword/findbyemail",MRAPI];
    }
    
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:strUrl parameters:dic_email success:^(id obj) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        NSString *str_false = obj[@"error"];
        
        NSLog(@"------%@－－－－%@",obj,obj[@"error"]);
        
        if (str_false.length==0)
        {
            if ([self.str_title isEqualToString:@"邮箱找回密码"])
            {
                [BaseViewController showMessage:@"已发往您的邮箱，请注意查看"];
                [self.navigationController popViewControllerAnimated:NO];
            }
            if ([self.str_title isEqualToString:@"邮箱绑定"])
            {
                [BaseViewController showMessage:@"获取验证码成功"];

                MailboxBindingSecondVC *mailboxView = [[MailboxBindingSecondVC alloc] init];
                mailboxView.str_capcha = self.textField_email.text;
                [self.navigationController pushViewController:mailboxView animated:YES];
                
            }
            if ([self.str_fromeWhere isEqualToString:@"tradePwd"])
            {
                [BaseViewController showMessage:@"已发往您的邮箱，请注意查看"];
                
                [self.navigationController popViewControllerAnimated:NO];
            }
        
        }
        else
        {
            [BaseViewController showMessage:str_false];
        }
        
    } fail:^(NSError *error) {
        [UIView showAlertMsg:@"网络请求超时"];
    }];
}

//下一步
//- (void) nextButtonAction
//{
//}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
